package com.cldz.service;

import com.cldz.common.dto.res.DeviceDataResDto;
import com.cldz.dao.DevDataMapper;
import com.cldz.utils.TimeBetUtil;
import com.cldz.common.dto.req.DevDataDto;

import com.cldz.entity.DeviceData;
import com.cldz.utils.BeanCopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 14:50 2018/12/12
 * @ Description：
 * @ Modified By：
 */
@Service("devDataService")
@Slf4j
public class DevDataService {

    @Autowired
    private DevDataMapper devDataMapper;

    public List<DeviceDataResDto> getDeviceRealData(DevDataDto devDataDto){
        List<DeviceData> deviceRealData = devDataMapper.getDeviceRealData(devDataDto);
        return copyListBean(deviceRealData);
    }

    public Integer getDeviceRealDataAmount(DevDataDto devDataDto) {

        return devDataMapper.getDeviceRealDataAmount(devDataDto);
    }



    public List<DeviceDataResDto> getHistoryData(List<Map<String, Object>> list) {

        List<DeviceData> devDataDtos = devDataMapper.getHistoryData(list);

        return copyListBean(devDataDtos);
    }

    public Integer getHistoryDataAmount(List<Map<String, Object>> list) {

        return devDataMapper.getHistoryDataAmount(list);
    }

    public boolean isTableExist(String tableName){
        Map<String,Object> map = devDataMapper.isTableExist(tableName);
        if(map != null){
            return true;
        }else {
            return false;
        }
    }


    public DevDataDto getDataByChannelID(DevDataDto devDataDto) {
        DeviceData data = devDataMapper.getDataByChannelID(devDataDto);
        DevDataDto resDto = new DevDataDto();

        if(data != null){
            BeanCopyUtil.copyPropertiesIgnoreNull(data,resDto);
        }
        return resDto;
    }





    private List<DeviceDataResDto> copyListBean(List<DeviceData> devListByTypeID) {
        List<DeviceDataResDto> list = new ArrayList<>();
        for (DeviceData device : devListByTypeID) {
            DeviceDataResDto dto = new DeviceDataResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }





}
